package com.lyc.mapper;

import com.lyc.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {
    
    /**
     * 根据帖子ID查询评论列表
     * @param postId 帖子ID
     * @return 评论列表
     */
    @Select("SELECT * FROM comments WHERE post_id = #{postId} ORDER BY created_at DESC")
    List<Comment> findByPostId(@Param("postId") Integer postId);
    
    /**
     * 查询所有评论
     * @return 评论列表
     */
    @Select("SELECT * FROM comments ORDER BY created_at DESC")
    List<Comment> findAll();
    
    /**
     * 插入新评论
     * @param comment 评论对象
     * @return 影响行数
     */
    @Insert("INSERT INTO comments (post_id, user_id, parent_id, content, status, created_at, updated_at) " +
            "VALUES (#{postId}, #{userId}, #{parentId}, #{content}, #{status}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Comment comment);

    /**
     *根据postid删除评论
     *@param postId 帖子ID
     * @return 影响行数
     */
    @Delete("DELETE FROM comments WHERE post_id = #{postId}")
    int deleteByPostId(@Param("postId") Integer postId);


    /**
     * 根据评论ID删除评论
     * @param id 评论ID
     * @return 影响行数
     */
    @Delete("DELETE FROM comments WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);
    
    /**
     * 根据父评论ID删除子评论
     * @param parentId 父评论ID
     * @return 影响行数
     */
    @Delete("DELETE FROM comments WHERE parent_id = #{parentId}")
    int deleteByParentId(@Param("parentId") Integer parentId);
    
    /**
     * 批量删除评论
     * @param ids 评论ID列表
     * @return 影响行数
     */
    @Delete("<script>" +
            "DELETE FROM comments WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteByIds(@Param("ids") List<Integer> ids);
    
    /**
     * 根据评论ID查询评论
     * @param id 评论ID
     * @return 评论对象
     */
    @Select("SELECT * FROM comments WHERE id = #{id}")
    Comment findById(@Param("id") Integer id);
    
    /**
     * 增加评论点赞数
     * @param id 评论ID
     * @return 影响行数
     */
    @Update("UPDATE comments SET like_count = like_count + 1 WHERE id = #{id}")
    int incrementLikeCount(@Param("id") Integer id);
    
    /**
     * 减少评论点赞数
     * @param id 评论ID
     * @return 影响行数
     */
    @Update("UPDATE comments SET like_count = like_count - 1 WHERE id = #{id} AND like_count > 0")
    int decrementLikeCount(@Param("id") Integer id);
}